{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "10148cf1-4599-4fa7-aa84-b16eb242aba4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import datetime as dt\n",
    "import math as m\n",
    "import matplotlib.pyplot as plt\n",
    "from dateutil.relativedelta import *\n",
    "from pandas.tseries.offsets import *\n",
    "from scipy import stats\n",
    "from functools import partial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "419903e5-ca79-42d9-a974-2a77f7ea5db8",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>date</th>\n",
       "      <th>stock</th>\n",
       "      <th>ret</th>\n",
       "      <th>Rf</th>\n",
       "      <th>mv</th>\n",
       "      <th>assets</th>\n",
       "      <th>b_v</th>\n",
       "      <th>n_p</th>\n",
       "      <th>c_f</th>\n",
       "      <th>vol</th>\n",
       "      <th>max</th>\n",
       "      <th>y_tur</th>\n",
       "      <th>m_tur</th>\n",
       "      <th>rev</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>2000/1/31</td>\n",
       "      <td>1</td>\n",
       "      <td>0.061891</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>2.875573e+10</td>\n",
       "      <td>4.973234e+10</td>\n",
       "      <td>3.078513e+09</td>\n",
       "      <td>2.965881e+07</td>\n",
       "      <td>-2.822422e+08</td>\n",
       "      <td>0.028339</td>\n",
       "      <td>0.048138</td>\n",
       "      <td>0.010272</td>\n",
       "      <td>0.853529</td>\n",
       "      <td>0.067390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2000/1/31</td>\n",
       "      <td>2</td>\n",
       "      <td>0.161126</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>5.372237e+09</td>\n",
       "      <td>5.045245e+09</td>\n",
       "      <td>2.887902e+09</td>\n",
       "      <td>1.998272e+07</td>\n",
       "      <td>2.517391e+07</td>\n",
       "      <td>0.042418</td>\n",
       "      <td>0.117017</td>\n",
       "      <td>0.012503</td>\n",
       "      <td>1.152299</td>\n",
       "      <td>0.165757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2000/1/31</td>\n",
       "      <td>6</td>\n",
       "      <td>0.161734</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>2.786972e+09</td>\n",
       "      <td>3.727572e+09</td>\n",
       "      <td>1.227442e+09</td>\n",
       "      <td>6.568793e+06</td>\n",
       "      <td>1.803695e+07</td>\n",
       "      <td>0.037841</td>\n",
       "      <td>0.100100</td>\n",
       "      <td>0.007577</td>\n",
       "      <td>1.161986</td>\n",
       "      <td>0.163149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>2000/1/31</td>\n",
       "      <td>8</td>\n",
       "      <td>1.126714</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>6.625843e+09</td>\n",
       "      <td>3.090314e+08</td>\n",
       "      <td>1.382105e+08</td>\n",
       "      <td>4.661097e+06</td>\n",
       "      <td>1.967555e+06</td>\n",
       "      <td>0.029278</td>\n",
       "      <td>0.088629</td>\n",
       "      <td>0.013140</td>\n",
       "      <td>1.744705</td>\n",
       "      <td>0.777256</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>2000/1/31</td>\n",
       "      <td>11</td>\n",
       "      <td>-0.032110</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>3.040551e+09</td>\n",
       "      <td>2.455629e+09</td>\n",
       "      <td>2.159964e+08</td>\n",
       "      <td>1.859843e+06</td>\n",
       "      <td>8.769510e+05</td>\n",
       "      <td>0.029709</td>\n",
       "      <td>0.041494</td>\n",
       "      <td>0.026355</td>\n",
       "      <td>0.362418</td>\n",
       "      <td>-0.024600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>320055</th>\n",
       "      <td>320055</td>\n",
       "      <td>2020/12/31</td>\n",
       "      <td>603993</td>\n",
       "      <td>0.361656</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>8.108590e+10</td>\n",
       "      <td>1.220000e+11</td>\n",
       "      <td>4.733486e+10</td>\n",
       "      <td>2.569956e+08</td>\n",
       "      <td>-2.591281e+09</td>\n",
       "      <td>0.046480</td>\n",
       "      <td>0.101075</td>\n",
       "      <td>0.012288</td>\n",
       "      <td>1.953591</td>\n",
       "      <td>0.333853</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>320056</th>\n",
       "      <td>320056</td>\n",
       "      <td>2020/12/31</td>\n",
       "      <td>603995</td>\n",
       "      <td>-0.109514</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>6.740177e+09</td>\n",
       "      <td>6.206492e+09</td>\n",
       "      <td>3.609950e+09</td>\n",
       "      <td>6.199818e+07</td>\n",
       "      <td>-1.596543e+07</td>\n",
       "      <td>0.021471</td>\n",
       "      <td>0.041256</td>\n",
       "      <td>0.063802</td>\n",
       "      <td>0.334396</td>\n",
       "      <td>-0.110572</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>320057</th>\n",
       "      <td>320057</td>\n",
       "      <td>2020/12/31</td>\n",
       "      <td>603997</td>\n",
       "      <td>-0.126304</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>8.813384e+09</td>\n",
       "      <td>1.732582e+10</td>\n",
       "      <td>4.567113e+09</td>\n",
       "      <td>6.340777e+06</td>\n",
       "      <td>1.015194e+08</td>\n",
       "      <td>0.022907</td>\n",
       "      <td>0.048689</td>\n",
       "      <td>0.016032</td>\n",
       "      <td>0.381120</td>\n",
       "      <td>-0.128861</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>320058</th>\n",
       "      <td>320058</td>\n",
       "      <td>2020/12/31</td>\n",
       "      <td>603998</td>\n",
       "      <td>-0.049206</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>2.705407e+09</td>\n",
       "      <td>2.242781e+09</td>\n",
       "      <td>1.265731e+09</td>\n",
       "      <td>-1.201622e+06</td>\n",
       "      <td>6.125389e+07</td>\n",
       "      <td>0.013304</td>\n",
       "      <td>0.024768</td>\n",
       "      <td>0.012695</td>\n",
       "      <td>0.505311</td>\n",
       "      <td>-0.048452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>320059</th>\n",
       "      <td>320059</td>\n",
       "      <td>2020/12/31</td>\n",
       "      <td>603999</td>\n",
       "      <td>-0.123810</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>3.628800e+09</td>\n",
       "      <td>2.137164e+09</td>\n",
       "      <td>1.781097e+09</td>\n",
       "      <td>5.893884e+06</td>\n",
       "      <td>3.631815e+07</td>\n",
       "      <td>0.013780</td>\n",
       "      <td>0.014898</td>\n",
       "      <td>0.012777</td>\n",
       "      <td>0.440671</td>\n",
       "      <td>-0.129685</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>320060 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        Unnamed: 0        date   stock       ret        Rf            mv  \\\n",
       "0                0   2000/1/31       1  0.061891  0.001856  2.875573e+10   \n",
       "1                1   2000/1/31       2  0.161126  0.001856  5.372237e+09   \n",
       "2                2   2000/1/31       6  0.161734  0.001856  2.786972e+09   \n",
       "3                3   2000/1/31       8  1.126714  0.001856  6.625843e+09   \n",
       "4                4   2000/1/31      11 -0.032110  0.001856  3.040551e+09   \n",
       "...            ...         ...     ...       ...       ...           ...   \n",
       "320055      320055  2020/12/31  603993  0.361656  0.001241  8.108590e+10   \n",
       "320056      320056  2020/12/31  603995 -0.109514  0.001241  6.740177e+09   \n",
       "320057      320057  2020/12/31  603997 -0.126304  0.001241  8.813384e+09   \n",
       "320058      320058  2020/12/31  603998 -0.049206  0.001241  2.705407e+09   \n",
       "320059      320059  2020/12/31  603999 -0.123810  0.001241  3.628800e+09   \n",
       "\n",
       "              assets           b_v           n_p           c_f       vol  \\\n",
       "0       4.973234e+10  3.078513e+09  2.965881e+07 -2.822422e+08  0.028339   \n",
       "1       5.045245e+09  2.887902e+09  1.998272e+07  2.517391e+07  0.042418   \n",
       "2       3.727572e+09  1.227442e+09  6.568793e+06  1.803695e+07  0.037841   \n",
       "3       3.090314e+08  1.382105e+08  4.661097e+06  1.967555e+06  0.029278   \n",
       "4       2.455629e+09  2.159964e+08  1.859843e+06  8.769510e+05  0.029709   \n",
       "...              ...           ...           ...           ...       ...   \n",
       "320055  1.220000e+11  4.733486e+10  2.569956e+08 -2.591281e+09  0.046480   \n",
       "320056  6.206492e+09  3.609950e+09  6.199818e+07 -1.596543e+07  0.021471   \n",
       "320057  1.732582e+10  4.567113e+09  6.340777e+06  1.015194e+08  0.022907   \n",
       "320058  2.242781e+09  1.265731e+09 -1.201622e+06  6.125389e+07  0.013304   \n",
       "320059  2.137164e+09  1.781097e+09  5.893884e+06  3.631815e+07  0.013780   \n",
       "\n",
       "             max     y_tur     m_tur       rev  \n",
       "0       0.048138  0.010272  0.853529  0.067390  \n",
       "1       0.117017  0.012503  1.152299  0.165757  \n",
       "2       0.100100  0.007577  1.161986  0.163149  \n",
       "3       0.088629  0.013140  1.744705  0.777256  \n",
       "4       0.041494  0.026355  0.362418 -0.024600  \n",
       "...          ...       ...       ...       ...  \n",
       "320055  0.101075  0.012288  1.953591  0.333853  \n",
       "320056  0.041256  0.063802  0.334396 -0.110572  \n",
       "320057  0.048689  0.016032  0.381120 -0.128861  \n",
       "320058  0.024768  0.012695  0.505311 -0.048452  \n",
       "320059  0.014898  0.012777  0.440671 -0.129685  \n",
       "\n",
       "[320060 rows x 15 columns]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "das = pd.read_csv(\"D:\\\\资产定价\\\\data数据\\\\data数据\\\\70%股票月度数据.csv\")\n",
    "das"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "0e044b8a-3f17-4eec-9ec4-c22073106fcf",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>date</th>\n",
       "      <th>stock</th>\n",
       "      <th>ret</th>\n",
       "      <th>Rf</th>\n",
       "      <th>mv</th>\n",
       "      <th>assets</th>\n",
       "      <th>b_v</th>\n",
       "      <th>n_p</th>\n",
       "      <th>c_f</th>\n",
       "      <th>vol</th>\n",
       "      <th>max</th>\n",
       "      <th>y_tur</th>\n",
       "      <th>m_tur</th>\n",
       "      <th>rev</th>\n",
       "      <th>ep</th>\n",
       "      <th>bm</th>\n",
       "      <th>cp</th>\n",
       "      <th>roe</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>2000-01-31</td>\n",
       "      <td>1</td>\n",
       "      <td>0.061891</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>2.875573e+10</td>\n",
       "      <td>4.973234e+10</td>\n",
       "      <td>3.078513e+09</td>\n",
       "      <td>2.965881e+07</td>\n",
       "      <td>-2.822422e+08</td>\n",
       "      <td>0.028339</td>\n",
       "      <td>0.048138</td>\n",
       "      <td>0.010272</td>\n",
       "      <td>0.853529</td>\n",
       "      <td>0.067390</td>\n",
       "      <td>0.001031</td>\n",
       "      <td>0.107057</td>\n",
       "      <td>-0.009815</td>\n",
       "      <td>0.009634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2000-01-31</td>\n",
       "      <td>2</td>\n",
       "      <td>0.161126</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>5.372237e+09</td>\n",
       "      <td>5.045245e+09</td>\n",
       "      <td>2.887902e+09</td>\n",
       "      <td>1.998272e+07</td>\n",
       "      <td>2.517391e+07</td>\n",
       "      <td>0.042418</td>\n",
       "      <td>0.117017</td>\n",
       "      <td>0.012503</td>\n",
       "      <td>1.152299</td>\n",
       "      <td>0.165757</td>\n",
       "      <td>0.003720</td>\n",
       "      <td>0.537560</td>\n",
       "      <td>0.004686</td>\n",
       "      <td>0.006919</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2000-01-31</td>\n",
       "      <td>6</td>\n",
       "      <td>0.161734</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>2.786972e+09</td>\n",
       "      <td>3.727572e+09</td>\n",
       "      <td>1.227442e+09</td>\n",
       "      <td>6.568793e+06</td>\n",
       "      <td>1.803695e+07</td>\n",
       "      <td>0.037841</td>\n",
       "      <td>0.100100</td>\n",
       "      <td>0.007577</td>\n",
       "      <td>1.161986</td>\n",
       "      <td>0.163149</td>\n",
       "      <td>0.002357</td>\n",
       "      <td>0.440421</td>\n",
       "      <td>0.006472</td>\n",
       "      <td>0.005352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>2000-01-31</td>\n",
       "      <td>8</td>\n",
       "      <td>1.126714</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>6.625843e+09</td>\n",
       "      <td>3.090314e+08</td>\n",
       "      <td>1.382105e+08</td>\n",
       "      <td>4.661097e+06</td>\n",
       "      <td>1.967555e+06</td>\n",
       "      <td>0.029278</td>\n",
       "      <td>0.088629</td>\n",
       "      <td>0.013140</td>\n",
       "      <td>1.744705</td>\n",
       "      <td>0.777256</td>\n",
       "      <td>0.000703</td>\n",
       "      <td>0.020859</td>\n",
       "      <td>0.000297</td>\n",
       "      <td>0.033725</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>2000-01-31</td>\n",
       "      <td>11</td>\n",
       "      <td>-0.032110</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>3.040551e+09</td>\n",
       "      <td>2.455629e+09</td>\n",
       "      <td>2.159964e+08</td>\n",
       "      <td>1.859843e+06</td>\n",
       "      <td>8.769510e+05</td>\n",
       "      <td>0.029709</td>\n",
       "      <td>0.041494</td>\n",
       "      <td>0.026355</td>\n",
       "      <td>0.362418</td>\n",
       "      <td>-0.024600</td>\n",
       "      <td>0.000612</td>\n",
       "      <td>0.071039</td>\n",
       "      <td>0.000288</td>\n",
       "      <td>0.008611</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>320055</th>\n",
       "      <td>320055</td>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>603993</td>\n",
       "      <td>0.361656</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>8.108590e+10</td>\n",
       "      <td>1.220000e+11</td>\n",
       "      <td>4.733486e+10</td>\n",
       "      <td>2.569956e+08</td>\n",
       "      <td>-2.591281e+09</td>\n",
       "      <td>0.046480</td>\n",
       "      <td>0.101075</td>\n",
       "      <td>0.012288</td>\n",
       "      <td>1.953591</td>\n",
       "      <td>0.333853</td>\n",
       "      <td>0.003169</td>\n",
       "      <td>0.583762</td>\n",
       "      <td>-0.031957</td>\n",
       "      <td>0.005429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>320056</th>\n",
       "      <td>320056</td>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>603995</td>\n",
       "      <td>-0.109514</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>6.740177e+09</td>\n",
       "      <td>6.206492e+09</td>\n",
       "      <td>3.609950e+09</td>\n",
       "      <td>6.199818e+07</td>\n",
       "      <td>-1.596543e+07</td>\n",
       "      <td>0.021471</td>\n",
       "      <td>0.041256</td>\n",
       "      <td>0.063802</td>\n",
       "      <td>0.334396</td>\n",
       "      <td>-0.110572</td>\n",
       "      <td>0.009198</td>\n",
       "      <td>0.535587</td>\n",
       "      <td>-0.002369</td>\n",
       "      <td>0.017174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>320057</th>\n",
       "      <td>320057</td>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>603997</td>\n",
       "      <td>-0.126304</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>8.813384e+09</td>\n",
       "      <td>1.732582e+10</td>\n",
       "      <td>4.567113e+09</td>\n",
       "      <td>6.340777e+06</td>\n",
       "      <td>1.015194e+08</td>\n",
       "      <td>0.022907</td>\n",
       "      <td>0.048689</td>\n",
       "      <td>0.016032</td>\n",
       "      <td>0.381120</td>\n",
       "      <td>-0.128861</td>\n",
       "      <td>0.000719</td>\n",
       "      <td>0.518202</td>\n",
       "      <td>0.011519</td>\n",
       "      <td>0.001388</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>320058</th>\n",
       "      <td>320058</td>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>603998</td>\n",
       "      <td>-0.049206</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>2.705407e+09</td>\n",
       "      <td>2.242781e+09</td>\n",
       "      <td>1.265731e+09</td>\n",
       "      <td>-1.201622e+06</td>\n",
       "      <td>6.125389e+07</td>\n",
       "      <td>0.013304</td>\n",
       "      <td>0.024768</td>\n",
       "      <td>0.012695</td>\n",
       "      <td>0.505311</td>\n",
       "      <td>-0.048452</td>\n",
       "      <td>-0.000444</td>\n",
       "      <td>0.467852</td>\n",
       "      <td>0.022641</td>\n",
       "      <td>-0.000949</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>320059</th>\n",
       "      <td>320059</td>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>603999</td>\n",
       "      <td>-0.123810</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>3.628800e+09</td>\n",
       "      <td>2.137164e+09</td>\n",
       "      <td>1.781097e+09</td>\n",
       "      <td>5.893884e+06</td>\n",
       "      <td>3.631815e+07</td>\n",
       "      <td>0.013780</td>\n",
       "      <td>0.014898</td>\n",
       "      <td>0.012777</td>\n",
       "      <td>0.440671</td>\n",
       "      <td>-0.129685</td>\n",
       "      <td>0.001624</td>\n",
       "      <td>0.490823</td>\n",
       "      <td>0.010008</td>\n",
       "      <td>0.003309</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>319705 rows × 19 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        Unnamed: 0       date   stock       ret        Rf            mv  \\\n",
       "0                0 2000-01-31       1  0.061891  0.001856  2.875573e+10   \n",
       "1                1 2000-01-31       2  0.161126  0.001856  5.372237e+09   \n",
       "2                2 2000-01-31       6  0.161734  0.001856  2.786972e+09   \n",
       "3                3 2000-01-31       8  1.126714  0.001856  6.625843e+09   \n",
       "4                4 2000-01-31      11 -0.032110  0.001856  3.040551e+09   \n",
       "...            ...        ...     ...       ...       ...           ...   \n",
       "320055      320055 2020-12-31  603993  0.361656  0.001241  8.108590e+10   \n",
       "320056      320056 2020-12-31  603995 -0.109514  0.001241  6.740177e+09   \n",
       "320057      320057 2020-12-31  603997 -0.126304  0.001241  8.813384e+09   \n",
       "320058      320058 2020-12-31  603998 -0.049206  0.001241  2.705407e+09   \n",
       "320059      320059 2020-12-31  603999 -0.123810  0.001241  3.628800e+09   \n",
       "\n",
       "              assets           b_v           n_p           c_f       vol  \\\n",
       "0       4.973234e+10  3.078513e+09  2.965881e+07 -2.822422e+08  0.028339   \n",
       "1       5.045245e+09  2.887902e+09  1.998272e+07  2.517391e+07  0.042418   \n",
       "2       3.727572e+09  1.227442e+09  6.568793e+06  1.803695e+07  0.037841   \n",
       "3       3.090314e+08  1.382105e+08  4.661097e+06  1.967555e+06  0.029278   \n",
       "4       2.455629e+09  2.159964e+08  1.859843e+06  8.769510e+05  0.029709   \n",
       "...              ...           ...           ...           ...       ...   \n",
       "320055  1.220000e+11  4.733486e+10  2.569956e+08 -2.591281e+09  0.046480   \n",
       "320056  6.206492e+09  3.609950e+09  6.199818e+07 -1.596543e+07  0.021471   \n",
       "320057  1.732582e+10  4.567113e+09  6.340777e+06  1.015194e+08  0.022907   \n",
       "320058  2.242781e+09  1.265731e+09 -1.201622e+06  6.125389e+07  0.013304   \n",
       "320059  2.137164e+09  1.781097e+09  5.893884e+06  3.631815e+07  0.013780   \n",
       "\n",
       "             max     y_tur     m_tur       rev        ep        bm        cp  \\\n",
       "0       0.048138  0.010272  0.853529  0.067390  0.001031  0.107057 -0.009815   \n",
       "1       0.117017  0.012503  1.152299  0.165757  0.003720  0.537560  0.004686   \n",
       "2       0.100100  0.007577  1.161986  0.163149  0.002357  0.440421  0.006472   \n",
       "3       0.088629  0.013140  1.744705  0.777256  0.000703  0.020859  0.000297   \n",
       "4       0.041494  0.026355  0.362418 -0.024600  0.000612  0.071039  0.000288   \n",
       "...          ...       ...       ...       ...       ...       ...       ...   \n",
       "320055  0.101075  0.012288  1.953591  0.333853  0.003169  0.583762 -0.031957   \n",
       "320056  0.041256  0.063802  0.334396 -0.110572  0.009198  0.535587 -0.002369   \n",
       "320057  0.048689  0.016032  0.381120 -0.128861  0.000719  0.518202  0.011519   \n",
       "320058  0.024768  0.012695  0.505311 -0.048452 -0.000444  0.467852  0.022641   \n",
       "320059  0.014898  0.012777  0.440671 -0.129685  0.001624  0.490823  0.010008   \n",
       "\n",
       "             roe  \n",
       "0       0.009634  \n",
       "1       0.006919  \n",
       "2       0.005352  \n",
       "3       0.033725  \n",
       "4       0.008611  \n",
       "...          ...  \n",
       "320055  0.005429  \n",
       "320056  0.017174  \n",
       "320057  0.001388  \n",
       "320058 -0.000949  \n",
       "320059  0.003309  \n",
       "\n",
       "[319705 rows x 19 columns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "das['date']=pd.to_datetime(das['date'])\n",
    "das['ep'] =das.n_p/das.mv\n",
    "das['bm'] =das.b_v/das.mv\n",
    "das['cp']=das.c_f/das.mv\n",
    "das['roe']= das.n_p/das.b_v\n",
    "das=das.dropna()\n",
    "das"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "01099562-3197-4d2d-be93-7cb5d0c48486",
   "metadata": {},
   "outputs": [],
   "source": [
    "def mkt(f):\n",
    "    mkt = (f['ret']-f['Rf'])*f['mv']/f['mv'].sum()\n",
    "    f['MKT']=mkt\n",
    "    return f \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "4296dc9d-1b99-4a9a-9c2a-de8174a03792",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\lenovo\\AppData\\Local\\Temp\\ipykernel_5800\\603566272.py:2: DeprecationWarning: DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.\n",
      "  datam = datam.groupby('date', as_index=False).apply(mkt)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>MKT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2000-01-31</td>\n",
       "      <td>0.176722</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2000-02-29</td>\n",
       "      <td>0.112557</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2000-03-31</td>\n",
       "      <td>0.048319</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2000-04-30</td>\n",
       "      <td>0.025977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2000-05-31</td>\n",
       "      <td>0.024340</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>247</th>\n",
       "      <td>2020-08-31</td>\n",
       "      <td>0.023324</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>248</th>\n",
       "      <td>2020-09-30</td>\n",
       "      <td>-0.057268</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>249</th>\n",
       "      <td>2020-10-31</td>\n",
       "      <td>0.009826</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>250</th>\n",
       "      <td>2020-11-30</td>\n",
       "      <td>0.038409</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>251</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>0.029981</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>252 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          date       MKT\n",
       "0   2000-01-31  0.176722\n",
       "1   2000-02-29  0.112557\n",
       "2   2000-03-31  0.048319\n",
       "3   2000-04-30  0.025977\n",
       "4   2000-05-31  0.024340\n",
       "..         ...       ...\n",
       "247 2020-08-31  0.023324\n",
       "248 2020-09-30 -0.057268\n",
       "249 2020-10-31  0.009826\n",
       "250 2020-11-30  0.038409\n",
       "251 2020-12-31  0.029981\n",
       "\n",
       "[252 rows x 2 columns]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#构造市场因子\n",
    "datam = das\n",
    "datam = datam.groupby('date', as_index=False).apply(mkt)#使用as_index=False避免date成为索引，这样后续就不需要reset_index\n",
    "datam = datam.groupby('date', as_index=False).sum()\n",
    "mktfactor = datam[['date','MKT']]\n",
    "mktfactor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "71074669-2b13-458b-9cc5-9379df48546c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#按日期将股票分组\n",
    "data_yf = das[['date', 'stock', 'mv', 'bm', 'ep']]\n",
    "\n",
    "# 计算每个日期的市值中位数（用于划分\"小盘/大盘\"股票）\n",
    "data_sz = data_yf.groupby(['date'])['mv'].median().reset_index().rename(columns={'mv': 'sizemedn'})\n",
    "\n",
    "# 计算每个日期ep的30%和70%分位数（用于划分ep的\"低/中/高\"组）\n",
    "data_ep = data_yf.groupby('date')['ep'].quantile([0.3, 0.7]).unstack().reset_index()\n",
    "data_ep.columns = ['date', 'ep30', 'ep70']  # 直接重命名列\n",
    "# 计算每个日期bm的30%和70%分位数（用于划分bm的\"低/中/高\"组）\n",
    "data_bm = data_yf.groupby(['date'])['bm'].describe(percentiles=[0.3, 0.7]).reset_index()\n",
    "data_bm = data_bm[['date', '30%', '70%']].rename(columns={'30%': 'bm30', '70%': 'bm70'})\n",
    "\n",
    "# 合并阈值数据：市值中位数+ep分位数 / 市值中位数+bm分位数\n",
    "data1_breaks = pd.merge(data_sz, data_ep, how='inner', on=['date'])\n",
    "data1_yf = pd.merge(data_yf, data1_breaks, how='right', on=['date'])  # 给原始数据添加ep分组阈值\n",
    "\n",
    "data2_breaks = pd.merge(data_sz, data_bm, how='inner', on=['date'])\n",
    "data2_yf = pd.merge(data_yf, data2_breaks, how='right', on=['date'])  # 给原始数据添加bm分组阈值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "3c2397c6-0c63-4d85-925e-37863cd670e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义股票分组函数:按市值划分：>中位数为B（大盘股）,否则为S（小盘股）\n",
    "def sz_bucket(row):\n",
    "    if pd.isna(row['mv']):\n",
    "        value = ''  # 处理缺失值\n",
    "    elif row['mv']> row['sizemedn']:\n",
    "        value = 'B'\n",
    "    else:\n",
    "        value = 'S'\n",
    "    return value\n",
    "\n",
    "# 按ep划分：<=30%分位数为G（成长），30%-70%为M（中性），>70%为V（价值）\n",
    "def ep_bucket(row):\n",
    "    if row['ep'] <= row['ep30']:\n",
    "        value = 'G'\n",
    "    elif row['ep'] <= row['ep70']:\n",
    "        value = 'M'\n",
    "    elif row['ep'] > row['ep70']:\n",
    "        value = 'V'\n",
    "    else:\n",
    "        value = ''\n",
    "    return value\n",
    "\n",
    "# 按bm划分：<=30%分位数为L（低），30%-70%为M（中），>70%为H（高）\n",
    "def bm_bucket(row):\n",
    "    if row['bm'] <= row['bm30']:\n",
    "        value = 'L'\n",
    "    elif row['bm'] <= row['bm70']:\n",
    "        value = 'M'\n",
    "    elif row['bm'] > row['bm70']:\n",
    "        value = 'H'\n",
    "    else:\n",
    "        value = ''\n",
    "    return value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "ecff768d-5ed9-4fee-bf61-fe69fcf9eb5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>stock</th>\n",
       "      <th>ret</th>\n",
       "      <th>Rf</th>\n",
       "      <th>mv</th>\n",
       "      <th>assets</th>\n",
       "      <th>b_v</th>\n",
       "      <th>n_p</th>\n",
       "      <th>c_f</th>\n",
       "      <th>vol</th>\n",
       "      <th>...</th>\n",
       "      <th>y_tur</th>\n",
       "      <th>m_tur</th>\n",
       "      <th>rev</th>\n",
       "      <th>ep</th>\n",
       "      <th>bm</th>\n",
       "      <th>cp</th>\n",
       "      <th>roe</th>\n",
       "      <th>szport</th>\n",
       "      <th>epport</th>\n",
       "      <th>bmport</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2000-01-31</td>\n",
       "      <td>1</td>\n",
       "      <td>0.061891</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>2.875573e+10</td>\n",
       "      <td>4.973234e+10</td>\n",
       "      <td>3.078513e+09</td>\n",
       "      <td>2.965881e+07</td>\n",
       "      <td>-2.822422e+08</td>\n",
       "      <td>0.028339</td>\n",
       "      <td>...</td>\n",
       "      <td>0.010272</td>\n",
       "      <td>0.853529</td>\n",
       "      <td>0.067390</td>\n",
       "      <td>0.001031</td>\n",
       "      <td>0.107057</td>\n",
       "      <td>-0.009815</td>\n",
       "      <td>0.009634</td>\n",
       "      <td>B</td>\n",
       "      <td>G</td>\n",
       "      <td>L</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2000-01-31</td>\n",
       "      <td>2</td>\n",
       "      <td>0.161126</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>5.372237e+09</td>\n",
       "      <td>5.045245e+09</td>\n",
       "      <td>2.887902e+09</td>\n",
       "      <td>1.998272e+07</td>\n",
       "      <td>2.517391e+07</td>\n",
       "      <td>0.042418</td>\n",
       "      <td>...</td>\n",
       "      <td>0.012503</td>\n",
       "      <td>1.152299</td>\n",
       "      <td>0.165757</td>\n",
       "      <td>0.003720</td>\n",
       "      <td>0.537560</td>\n",
       "      <td>0.004686</td>\n",
       "      <td>0.006919</td>\n",
       "      <td>B</td>\n",
       "      <td>V</td>\n",
       "      <td>H</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2000-01-31</td>\n",
       "      <td>6</td>\n",
       "      <td>0.161734</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>2.786972e+09</td>\n",
       "      <td>3.727572e+09</td>\n",
       "      <td>1.227442e+09</td>\n",
       "      <td>6.568793e+06</td>\n",
       "      <td>1.803695e+07</td>\n",
       "      <td>0.037841</td>\n",
       "      <td>...</td>\n",
       "      <td>0.007577</td>\n",
       "      <td>1.161986</td>\n",
       "      <td>0.163149</td>\n",
       "      <td>0.002357</td>\n",
       "      <td>0.440421</td>\n",
       "      <td>0.006472</td>\n",
       "      <td>0.005352</td>\n",
       "      <td>S</td>\n",
       "      <td>M</td>\n",
       "      <td>H</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2000-01-31</td>\n",
       "      <td>8</td>\n",
       "      <td>1.126714</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>6.625843e+09</td>\n",
       "      <td>3.090314e+08</td>\n",
       "      <td>1.382105e+08</td>\n",
       "      <td>4.661097e+06</td>\n",
       "      <td>1.967555e+06</td>\n",
       "      <td>0.029278</td>\n",
       "      <td>...</td>\n",
       "      <td>0.013140</td>\n",
       "      <td>1.744705</td>\n",
       "      <td>0.777256</td>\n",
       "      <td>0.000703</td>\n",
       "      <td>0.020859</td>\n",
       "      <td>0.000297</td>\n",
       "      <td>0.033725</td>\n",
       "      <td>B</td>\n",
       "      <td>G</td>\n",
       "      <td>L</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2000-01-31</td>\n",
       "      <td>11</td>\n",
       "      <td>-0.032110</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>3.040551e+09</td>\n",
       "      <td>2.455629e+09</td>\n",
       "      <td>2.159964e+08</td>\n",
       "      <td>1.859843e+06</td>\n",
       "      <td>8.769510e+05</td>\n",
       "      <td>0.029709</td>\n",
       "      <td>...</td>\n",
       "      <td>0.026355</td>\n",
       "      <td>0.362418</td>\n",
       "      <td>-0.024600</td>\n",
       "      <td>0.000612</td>\n",
       "      <td>0.071039</td>\n",
       "      <td>0.000288</td>\n",
       "      <td>0.008611</td>\n",
       "      <td>B</td>\n",
       "      <td>G</td>\n",
       "      <td>L</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>319700</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>603993</td>\n",
       "      <td>0.361656</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>8.108590e+10</td>\n",
       "      <td>1.220000e+11</td>\n",
       "      <td>4.733486e+10</td>\n",
       "      <td>2.569956e+08</td>\n",
       "      <td>-2.591281e+09</td>\n",
       "      <td>0.046480</td>\n",
       "      <td>...</td>\n",
       "      <td>0.012288</td>\n",
       "      <td>1.953591</td>\n",
       "      <td>0.333853</td>\n",
       "      <td>0.003169</td>\n",
       "      <td>0.583762</td>\n",
       "      <td>-0.031957</td>\n",
       "      <td>0.005429</td>\n",
       "      <td>B</td>\n",
       "      <td>M</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>319701</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>603995</td>\n",
       "      <td>-0.109514</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>6.740177e+09</td>\n",
       "      <td>6.206492e+09</td>\n",
       "      <td>3.609950e+09</td>\n",
       "      <td>6.199818e+07</td>\n",
       "      <td>-1.596543e+07</td>\n",
       "      <td>0.021471</td>\n",
       "      <td>...</td>\n",
       "      <td>0.063802</td>\n",
       "      <td>0.334396</td>\n",
       "      <td>-0.110572</td>\n",
       "      <td>0.009198</td>\n",
       "      <td>0.535587</td>\n",
       "      <td>-0.002369</td>\n",
       "      <td>0.017174</td>\n",
       "      <td>S</td>\n",
       "      <td>V</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>319702</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>603997</td>\n",
       "      <td>-0.126304</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>8.813384e+09</td>\n",
       "      <td>1.732582e+10</td>\n",
       "      <td>4.567113e+09</td>\n",
       "      <td>6.340777e+06</td>\n",
       "      <td>1.015194e+08</td>\n",
       "      <td>0.022907</td>\n",
       "      <td>...</td>\n",
       "      <td>0.016032</td>\n",
       "      <td>0.381120</td>\n",
       "      <td>-0.128861</td>\n",
       "      <td>0.000719</td>\n",
       "      <td>0.518202</td>\n",
       "      <td>0.011519</td>\n",
       "      <td>0.001388</td>\n",
       "      <td>S</td>\n",
       "      <td>G</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>319703</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>603998</td>\n",
       "      <td>-0.049206</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>2.705407e+09</td>\n",
       "      <td>2.242781e+09</td>\n",
       "      <td>1.265731e+09</td>\n",
       "      <td>-1.201622e+06</td>\n",
       "      <td>6.125389e+07</td>\n",
       "      <td>0.013304</td>\n",
       "      <td>...</td>\n",
       "      <td>0.012695</td>\n",
       "      <td>0.505311</td>\n",
       "      <td>-0.048452</td>\n",
       "      <td>-0.000444</td>\n",
       "      <td>0.467852</td>\n",
       "      <td>0.022641</td>\n",
       "      <td>-0.000949</td>\n",
       "      <td>S</td>\n",
       "      <td>G</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>319704</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>603999</td>\n",
       "      <td>-0.123810</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>3.628800e+09</td>\n",
       "      <td>2.137164e+09</td>\n",
       "      <td>1.781097e+09</td>\n",
       "      <td>5.893884e+06</td>\n",
       "      <td>3.631815e+07</td>\n",
       "      <td>0.013780</td>\n",
       "      <td>...</td>\n",
       "      <td>0.012777</td>\n",
       "      <td>0.440671</td>\n",
       "      <td>-0.129685</td>\n",
       "      <td>0.001624</td>\n",
       "      <td>0.490823</td>\n",
       "      <td>0.010008</td>\n",
       "      <td>0.003309</td>\n",
       "      <td>S</td>\n",
       "      <td>M</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>319705 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             date   stock       ret        Rf            mv        assets  \\\n",
       "0      2000-01-31       1  0.061891  0.001856  2.875573e+10  4.973234e+10   \n",
       "1      2000-01-31       2  0.161126  0.001856  5.372237e+09  5.045245e+09   \n",
       "2      2000-01-31       6  0.161734  0.001856  2.786972e+09  3.727572e+09   \n",
       "3      2000-01-31       8  1.126714  0.001856  6.625843e+09  3.090314e+08   \n",
       "4      2000-01-31      11 -0.032110  0.001856  3.040551e+09  2.455629e+09   \n",
       "...           ...     ...       ...       ...           ...           ...   \n",
       "319700 2020-12-31  603993  0.361656  0.001241  8.108590e+10  1.220000e+11   \n",
       "319701 2020-12-31  603995 -0.109514  0.001241  6.740177e+09  6.206492e+09   \n",
       "319702 2020-12-31  603997 -0.126304  0.001241  8.813384e+09  1.732582e+10   \n",
       "319703 2020-12-31  603998 -0.049206  0.001241  2.705407e+09  2.242781e+09   \n",
       "319704 2020-12-31  603999 -0.123810  0.001241  3.628800e+09  2.137164e+09   \n",
       "\n",
       "                 b_v           n_p           c_f       vol  ...     y_tur  \\\n",
       "0       3.078513e+09  2.965881e+07 -2.822422e+08  0.028339  ...  0.010272   \n",
       "1       2.887902e+09  1.998272e+07  2.517391e+07  0.042418  ...  0.012503   \n",
       "2       1.227442e+09  6.568793e+06  1.803695e+07  0.037841  ...  0.007577   \n",
       "3       1.382105e+08  4.661097e+06  1.967555e+06  0.029278  ...  0.013140   \n",
       "4       2.159964e+08  1.859843e+06  8.769510e+05  0.029709  ...  0.026355   \n",
       "...              ...           ...           ...       ...  ...       ...   \n",
       "319700  4.733486e+10  2.569956e+08 -2.591281e+09  0.046480  ...  0.012288   \n",
       "319701  3.609950e+09  6.199818e+07 -1.596543e+07  0.021471  ...  0.063802   \n",
       "319702  4.567113e+09  6.340777e+06  1.015194e+08  0.022907  ...  0.016032   \n",
       "319703  1.265731e+09 -1.201622e+06  6.125389e+07  0.013304  ...  0.012695   \n",
       "319704  1.781097e+09  5.893884e+06  3.631815e+07  0.013780  ...  0.012777   \n",
       "\n",
       "           m_tur       rev        ep        bm        cp       roe  szport  \\\n",
       "0       0.853529  0.067390  0.001031  0.107057 -0.009815  0.009634       B   \n",
       "1       1.152299  0.165757  0.003720  0.537560  0.004686  0.006919       B   \n",
       "2       1.161986  0.163149  0.002357  0.440421  0.006472  0.005352       S   \n",
       "3       1.744705  0.777256  0.000703  0.020859  0.000297  0.033725       B   \n",
       "4       0.362418 -0.024600  0.000612  0.071039  0.000288  0.008611       B   \n",
       "...          ...       ...       ...       ...       ...       ...     ...   \n",
       "319700  1.953591  0.333853  0.003169  0.583762 -0.031957  0.005429       B   \n",
       "319701  0.334396 -0.110572  0.009198  0.535587 -0.002369  0.017174       S   \n",
       "319702  0.381120 -0.128861  0.000719  0.518202  0.011519  0.001388       S   \n",
       "319703  0.505311 -0.048452 -0.000444  0.467852  0.022641 -0.000949       S   \n",
       "319704  0.440671 -0.129685  0.001624  0.490823  0.010008  0.003309       S   \n",
       "\n",
       "       epport bmport  \n",
       "0           G      L  \n",
       "1           V      H  \n",
       "2           M      H  \n",
       "3           G      L  \n",
       "4           G      L  \n",
       "...       ...    ...  \n",
       "319700      M      M  \n",
       "319701      V      M  \n",
       "319702      G      M  \n",
       "319703      G      M  \n",
       "319704      M      M  \n",
       "\n",
       "[319705 rows x 21 columns]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对data1_yf应用分组函数，得到市值分组(szport)和ep分组(epport)\n",
    "data1_yf = data1_yf.dropna()\n",
    "data1_yf['szport'] = data1_yf.apply(sz_bucket, axis=1)\n",
    "data1_yf['epport'] = data1_yf.apply(ep_bucket, axis=1)\n",
    "\n",
    "# 对data2_yf应用分组函数，得到市值分组(szport)和bm分组(bmport)\n",
    "data2_yf = data2_yf.dropna()\n",
    "data2_yf['szport'] = data2_yf.apply(sz_bucket, axis=1)\n",
    "data2_yf['bmport'] = data2_yf.apply(bm_bucket, axis=1)\n",
    "\n",
    "# 提取分组标签并合并到原始数据中\n",
    "yf1 = data1_yf.loc[:, ['stock', 'date', 'szport', 'epport']] \n",
    "yf2 = data2_yf.loc[:, ['stock', 'date', 'szport', 'bmport']]\n",
    "\n",
    "data1 = pd.merge(das, yf1, on=['date', 'stock'], how='right')  # 合并ep相关分组\n",
    "data2 = pd.merge(data1, yf2, on=['date', 'stock'], how='right')  # 合并bm相关分组\n",
    "data2=data2.drop(\"Unnamed: 0\",axis=1)\n",
    "data2=data2.drop('szport_y',axis=1)\n",
    "data2=data2.rename(columns={'szport_x':'szport'})#删除重复的列\n",
    "\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "157d4f3c-17dc-4880-8eee-5d6c860f3b86",
   "metadata": {},
   "outputs": [],
   "source": [
    "#计算加权平均值，SMB/HML需按市值加权股票收益率\n",
    "def wavg(group, avg_name, weight_name):\n",
    "    d = group[avg_name]  # 需要计算平均值的列（如收益率ret）\n",
    "    w = group[weight_name]  # 权重列（如市值mv）\n",
    "    try:\n",
    "        return (d * w).sum() / w.sum()  # 加权平均：(收益率*市值)之和 / 总市值\n",
    "    except ZeroDivisionError:\n",
    "        return np.nan  # 处理权重和为0的异常情况p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "e6e81fa9-9399-4b56-9faf-83441f86219f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\lenovo\\AppData\\Local\\Temp\\ipykernel_5800\\3774626298.py:2: DeprecationWarning: DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.\n",
      "  vwret = data2.groupby(['date', 'szport', 'epport']).apply(wavg, 'ret', 'mv').reset_index().rename(columns={0: 'vwret'})\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>sbport1</th>\n",
       "      <th>date</th>\n",
       "      <th>BG</th>\n",
       "      <th>BM</th>\n",
       "      <th>BV</th>\n",
       "      <th>SG</th>\n",
       "      <th>SM</th>\n",
       "      <th>SV</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2000-01-31</td>\n",
       "      <td>0.214373</td>\n",
       "      <td>0.262903</td>\n",
       "      <td>0.126991</td>\n",
       "      <td>0.095934</td>\n",
       "      <td>0.102538</td>\n",
       "      <td>0.104659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2000-02-29</td>\n",
       "      <td>0.134371</td>\n",
       "      <td>0.098545</td>\n",
       "      <td>0.105045</td>\n",
       "      <td>0.126449</td>\n",
       "      <td>0.141729</td>\n",
       "      <td>0.113398</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2000-03-31</td>\n",
       "      <td>0.037594</td>\n",
       "      <td>0.023866</td>\n",
       "      <td>0.020616</td>\n",
       "      <td>0.129586</td>\n",
       "      <td>0.099299</td>\n",
       "      <td>0.176867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2000-04-30</td>\n",
       "      <td>0.018310</td>\n",
       "      <td>0.014272</td>\n",
       "      <td>0.053640</td>\n",
       "      <td>0.009080</td>\n",
       "      <td>0.027907</td>\n",
       "      <td>0.035809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2000-05-31</td>\n",
       "      <td>-0.001958</td>\n",
       "      <td>0.008041</td>\n",
       "      <td>0.036120</td>\n",
       "      <td>0.067410</td>\n",
       "      <td>0.053698</td>\n",
       "      <td>0.049438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>247</th>\n",
       "      <td>2020-08-31</td>\n",
       "      <td>-0.010221</td>\n",
       "      <td>0.026161</td>\n",
       "      <td>0.030749</td>\n",
       "      <td>0.036351</td>\n",
       "      <td>0.047280</td>\n",
       "      <td>0.073854</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>248</th>\n",
       "      <td>2020-09-30</td>\n",
       "      <td>-0.077605</td>\n",
       "      <td>-0.063277</td>\n",
       "      <td>-0.036893</td>\n",
       "      <td>-0.066268</td>\n",
       "      <td>-0.074365</td>\n",
       "      <td>-0.070701</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>249</th>\n",
       "      <td>2020-10-31</td>\n",
       "      <td>-0.013545</td>\n",
       "      <td>0.013148</td>\n",
       "      <td>0.021683</td>\n",
       "      <td>-0.012681</td>\n",
       "      <td>-0.003680</td>\n",
       "      <td>0.006008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>250</th>\n",
       "      <td>2020-11-30</td>\n",
       "      <td>0.027873</td>\n",
       "      <td>0.007079</td>\n",
       "      <td>0.086801</td>\n",
       "      <td>0.016840</td>\n",
       "      <td>0.013608</td>\n",
       "      <td>0.050867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>251</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>0.028075</td>\n",
       "      <td>0.096073</td>\n",
       "      <td>-0.024468</td>\n",
       "      <td>-0.064203</td>\n",
       "      <td>-0.032590</td>\n",
       "      <td>-0.034472</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>252 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "sbport1       date        BG        BM        BV        SG        SM        SV\n",
       "0       2000-01-31  0.214373  0.262903  0.126991  0.095934  0.102538  0.104659\n",
       "1       2000-02-29  0.134371  0.098545  0.105045  0.126449  0.141729  0.113398\n",
       "2       2000-03-31  0.037594  0.023866  0.020616  0.129586  0.099299  0.176867\n",
       "3       2000-04-30  0.018310  0.014272  0.053640  0.009080  0.027907  0.035809\n",
       "4       2000-05-31 -0.001958  0.008041  0.036120  0.067410  0.053698  0.049438\n",
       "..             ...       ...       ...       ...       ...       ...       ...\n",
       "247     2020-08-31 -0.010221  0.026161  0.030749  0.036351  0.047280  0.073854\n",
       "248     2020-09-30 -0.077605 -0.063277 -0.036893 -0.066268 -0.074365 -0.070701\n",
       "249     2020-10-31 -0.013545  0.013148  0.021683 -0.012681 -0.003680  0.006008\n",
       "250     2020-11-30  0.027873  0.007079  0.086801  0.016840  0.013608  0.050867\n",
       "251     2020-12-31  0.028075  0.096073 -0.024468 -0.064203 -0.032590 -0.034472\n",
       "\n",
       "[252 rows x 7 columns]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按日期、市值分组、ep分组计算加权平均收益（vwret）\n",
    "vwret = data2.groupby(['date', 'szport', 'epport']).apply(wavg, 'ret', 'mv').reset_index().rename(columns={0: 'vwret'})\n",
    "vwret['sbport1'] = vwret['szport'] + vwret['epport']  # 组合标签（如SG=小盘成长、BV=大盘价值等）\n",
    "\n",
    "# 透视表：行=日期，列=组合标签，值=组合收益。透视表（Ch_factors）将数据转为 “宽表”：每行是一个日期，每列是一个组合的收益\n",
    "Ch_factors = vwret.pivot(index='date', columns='sbport1', values='vwret').reset_index()\n",
    "Ch_factors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "ef572f1a-b90a-4ec8-b431-e5b641591472",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算规模因子SMB（小盘减大盘）和价值因子VMG（价值减成长）\n",
    "Ch_factors['WB'] = (Ch_factors['BG'] + Ch_factors['BM'] + Ch_factors['BV']) / 3  # 大盘组合平均收益\n",
    "Ch_factors['WS'] = (Ch_factors['SG'] + Ch_factors['SM'] + Ch_factors['SV']) / 3  # 小盘组合平均收益\n",
    "Ch_factors['SMB'] = Ch_factors['WS'] - Ch_factors['WB']  # 小盘减大盘（规模因子）\n",
    "\n",
    "Ch_factors['WV'] = (Ch_factors['BV'] + Ch_factors['SV']) / 2  # 价值组合平均收益\n",
    "Ch_factors['WG'] = (Ch_factors['BG'] + Ch_factors['SG']) / 2  # 成长组合平均收益\n",
    "Ch_factors['VMG'] = Ch_factors['WV'] - Ch_factors['WG']  # 价值减成长（价值因子，基于ep）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "4fb4121b-10af-4581-88c5-5bf7e7015009",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\lenovo\\AppData\\Local\\Temp\\ipykernel_5800\\3246795782.py:2: DeprecationWarning: DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.\n",
      "  vwret1 = data2.groupby(['date', 'szport', 'bmport']).apply(wavg, 'ret', 'mv').reset_index().rename(columns={0: 'vwret'})\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>MKT</th>\n",
       "      <th>SMB</th>\n",
       "      <th>VMG</th>\n",
       "      <th>FFSMB</th>\n",
       "      <th>FFHML</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2000-01-31</td>\n",
       "      <td>0.176722</td>\n",
       "      <td>-0.100378</td>\n",
       "      <td>-0.039328</td>\n",
       "      <td>-0.075080</td>\n",
       "      <td>-0.193250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2000-02-29</td>\n",
       "      <td>0.112557</td>\n",
       "      <td>0.014538</td>\n",
       "      <td>-0.021189</td>\n",
       "      <td>0.024367</td>\n",
       "      <td>-0.036565</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2000-03-31</td>\n",
       "      <td>0.048319</td>\n",
       "      <td>0.107892</td>\n",
       "      <td>0.015151</td>\n",
       "      <td>0.099260</td>\n",
       "      <td>0.045611</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2000-04-30</td>\n",
       "      <td>0.025977</td>\n",
       "      <td>-0.004475</td>\n",
       "      <td>0.031030</td>\n",
       "      <td>-0.009765</td>\n",
       "      <td>0.031099</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2000-05-31</td>\n",
       "      <td>0.024340</td>\n",
       "      <td>0.042781</td>\n",
       "      <td>0.010053</td>\n",
       "      <td>0.042805</td>\n",
       "      <td>0.016834</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>247</th>\n",
       "      <td>2020-08-31</td>\n",
       "      <td>0.023324</td>\n",
       "      <td>0.036932</td>\n",
       "      <td>0.039237</td>\n",
       "      <td>0.027134</td>\n",
       "      <td>0.002288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>248</th>\n",
       "      <td>2020-09-30</td>\n",
       "      <td>-0.057268</td>\n",
       "      <td>-0.011187</td>\n",
       "      <td>0.018140</td>\n",
       "      <td>-0.014961</td>\n",
       "      <td>0.016348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>249</th>\n",
       "      <td>2020-10-31</td>\n",
       "      <td>0.009826</td>\n",
       "      <td>-0.010546</td>\n",
       "      <td>0.026959</td>\n",
       "      <td>-0.015288</td>\n",
       "      <td>-0.010900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>250</th>\n",
       "      <td>2020-11-30</td>\n",
       "      <td>0.038409</td>\n",
       "      <td>-0.013479</td>\n",
       "      <td>0.046477</td>\n",
       "      <td>-0.022531</td>\n",
       "      <td>0.090444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>251</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>0.029981</td>\n",
       "      <td>-0.076982</td>\n",
       "      <td>-0.011406</td>\n",
       "      <td>-0.071244</td>\n",
       "      <td>-0.073800</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>252 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          date       MKT       SMB       VMG     FFSMB     FFHML\n",
       "0   2000-01-31  0.176722 -0.100378 -0.039328 -0.075080 -0.193250\n",
       "1   2000-02-29  0.112557  0.014538 -0.021189  0.024367 -0.036565\n",
       "2   2000-03-31  0.048319  0.107892  0.015151  0.099260  0.045611\n",
       "3   2000-04-30  0.025977 -0.004475  0.031030 -0.009765  0.031099\n",
       "4   2000-05-31  0.024340  0.042781  0.010053  0.042805  0.016834\n",
       "..         ...       ...       ...       ...       ...       ...\n",
       "247 2020-08-31  0.023324  0.036932  0.039237  0.027134  0.002288\n",
       "248 2020-09-30 -0.057268 -0.011187  0.018140 -0.014961  0.016348\n",
       "249 2020-10-31  0.009826 -0.010546  0.026959 -0.015288 -0.010900\n",
       "250 2020-11-30  0.038409 -0.013479  0.046477 -0.022531  0.090444\n",
       "251 2020-12-31  0.029981 -0.076982 -0.011406 -0.071244 -0.073800\n",
       "\n",
       "[252 rows x 6 columns]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按日期、市值分组、bm分组计算加权平均收益（vwret）\n",
    "vwret1 = data2.groupby(['date', 'szport', 'bmport']).apply(wavg, 'ret', 'mv').reset_index().rename(columns={0: 'vwret'})\n",
    "vwret1['sbport2'] = vwret1['szport'] + vwret1['bmport']  # 组合标签（如SL=小盘低bm、BH=大盘高bm等）\n",
    "\n",
    "# 透视表：行=日期，列=组合标签，值=组合收益\n",
    "ff_factors = vwret1.pivot(index='date', columns='sbport2', values='vwret').reset_index()\n",
    "\n",
    "# 计算规模因子FFSMB和价值因子FFHML\n",
    "ff_factors['ff_WB'] = (ff_factors['BL'] + ff_factors['BM'] + ff_factors['BH']) / 3  # 大盘组合平均收益\n",
    "ff_factors['ff_WS'] = (ff_factors['SL'] + ff_factors['SM'] + ff_factors['SH']) / 3  # 小盘组合平均收益\n",
    "ff_factors['FFSMB'] = ff_factors['ff_WS'] - ff_factors['ff_WB']  # 小盘减大盘（规模因子，Fama-French版）\n",
    "\n",
    "ff_factors['WH'] = (ff_factors['BH'] + ff_factors['SH']) / 2  # 高bm组合平均收益\n",
    "ff_factors['WL'] = (ff_factors['BL'] + ff_factors['SL']) / 2  # 低bm组合平均收益\n",
    "ff_factors['FFHML'] = ff_factors['WH'] - ff_factors['WL']  # 高bm减低bm（价值因子，Fama-French版）\n",
    "\n",
    "# 合并所有因子（市场因子+基于ep的因子+基于bm的因子）\n",
    "data3 = pd.merge(Ch_factors, ff_factors, on='date', how='inner')\n",
    "data4 = pd.merge(data3, mktfactor, on='date', how='inner')\n",
    "Ch_ff = data4[['date', 'MKT', 'SMB', 'VMG', 'FFSMB', 'FFHML']]  # 最终因子数据集\n",
    "Ch_ff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "ff113bb9-bec3-4d0f-958e-d60b7d6a9583",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MKT</th>\n",
       "      <th>SMB</th>\n",
       "      <th>VMG</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>MKT</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.124801</td>\n",
       "      <td>-0.262362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SMB</th>\n",
       "      <td>0.124801</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.552867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>VMG</th>\n",
       "      <td>-0.262362</td>\n",
       "      <td>-0.552867</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          MKT       SMB       VMG\n",
       "MKT  1.000000  0.124801 -0.262362\n",
       "SMB  0.124801  1.000000 -0.552867\n",
       "VMG -0.262362 -0.552867  1.000000"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor_correlation=Ch_ff[['MKT','SMB','VMG']]\n",
    "factor_correlation.corr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "c518ad30-4026-457f-bad1-a5db44530868",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MKT    0.008217\n",
       "SMB    0.013100\n",
       "VMG    0.017390\n",
       "dtype: float64"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor_correlation[['MKT','SMB','VMG']].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "b8101bbc-c373-49ea-83dd-b7418cb28231",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MKT    0.076844\n",
       "SMB    0.047708\n",
       "VMG    0.036570\n",
       "dtype: float64"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor_correlation[['MKT','SMB','VMG']].std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "33e25d3f-07f7-4fb1-ac7c-bb54b2214ac8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#单样本t检验\n",
    "import scipy.stats as st"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "4b4f78d8-743f-4eea-8b9d-b82e018244c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TtestResult(statistic=1.6974890658041193, pvalue=0.09084369800901602, df=251)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1=st.ttest_1samp(Ch_ff['MKT'], 0)\n",
    "t1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "16671764-1783-45bc-9b4b-8861d1877379",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TtestResult(statistic=4.3588552707124935, pvalue=1.9083456193442502e-05, df=251)"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2=st.ttest_1samp(Ch_ff['SMB'], 0)\n",
    "t2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "7d827d62-7e9d-4467-b4d4-e0238eebe45b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TtestResult(statistic=7.548763208784489, pvalue=8.120792796849796e-13, df=251)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t3=st.ttest_1samp(Ch_ff['VMG'], 0)\n",
    "t3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "8dc43a92-c2b7-4454-8559-8e784afab514",
   "metadata": {},
   "outputs": [],
   "source": [
    "#表5：CH3与FF3相互解释（OLS）\n",
    "from statsmodels.formula.api import ols\n",
    "import statsmodels.api as sm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "624651b5-dd3a-4a09-a9db-53cea30a7c30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>OLS Regression Results</caption>\n",
       "<tr>\n",
       "  <th>Dep. Variable:</th>          <td>FFSMB</td>      <th>  R-squared:         </th> <td>   0.981</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model:</th>                   <td>OLS</td>       <th>  Adj. R-squared:    </th> <td>   0.981</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Method:</th>             <td>Least Squares</td>  <th>  F-statistic:       </th> <td>   6568.</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Date:</th>             <td>Wed, 06 Aug 2025</td> <th>  Prob (F-statistic):</th> <td>3.63e-216</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Time:</th>                 <td>17:21:16</td>     <th>  Log-Likelihood:    </th> <td>  899.60</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>No. Observations:</th>      <td>   252</td>      <th>  AIC:               </th> <td>  -1793.</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Residuals:</th>          <td>   249</td>      <th>  BIC:               </th> <td>  -1783.</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Model:</th>              <td>     2</td>      <th>                     </th>     <td> </td>    \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Covariance Type:</th>      <td>nonrobust</td>    <th>                     </th>     <td> </td>    \n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "      <td></td>         <th>coef</th>     <th>std err</th>      <th>t</th>      <th>P>|t|</th>  <th>[0.025</th>    <th>0.975]</th>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Intercept</th> <td>    0.0005</td> <td>    0.001</td> <td>    0.925</td> <td> 0.356</td> <td>   -0.001</td> <td>    0.002</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>SMB</th>       <td>    0.9393</td> <td>    0.011</td> <td>   86.299</td> <td> 0.000</td> <td>    0.918</td> <td>    0.961</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>VMG</th>       <td>   -0.2150</td> <td>    0.014</td> <td>  -15.140</td> <td> 0.000</td> <td>   -0.243</td> <td>   -0.187</td>\n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "  <th>Omnibus:</th>       <td>96.463</td> <th>  Durbin-Watson:     </th> <td>   1.712</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Prob(Omnibus):</th> <td> 0.000</td> <th>  Jarque-Bera (JB):  </th> <td>1244.349</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Skew:</th>          <td> 1.117</td> <th>  Prob(JB):          </th> <td>6.21e-271</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Kurtosis:</th>      <td>13.654</td> <th>  Cond. No.          </th> <td>    37.0</td> \n",
       "</tr>\n",
       "</table><br/><br/>Notes:<br/>[1] Standard Errors assume that the covariance matrix of the errors is correctly specified."
      ],
      "text/latex": [
       "\\begin{center}\n",
       "\\begin{tabular}{lclc}\n",
       "\\toprule\n",
       "\\textbf{Dep. Variable:}    &      FFSMB       & \\textbf{  R-squared:         } &     0.981   \\\\\n",
       "\\textbf{Model:}            &       OLS        & \\textbf{  Adj. R-squared:    } &     0.981   \\\\\n",
       "\\textbf{Method:}           &  Least Squares   & \\textbf{  F-statistic:       } &     6568.   \\\\\n",
       "\\textbf{Date:}             & Wed, 06 Aug 2025 & \\textbf{  Prob (F-statistic):} & 3.63e-216   \\\\\n",
       "\\textbf{Time:}             &     17:21:16     & \\textbf{  Log-Likelihood:    } &    899.60   \\\\\n",
       "\\textbf{No. Observations:} &         252      & \\textbf{  AIC:               } &    -1793.   \\\\\n",
       "\\textbf{Df Residuals:}     &         249      & \\textbf{  BIC:               } &    -1783.   \\\\\n",
       "\\textbf{Df Model:}         &           2      & \\textbf{                     } &             \\\\\n",
       "\\textbf{Covariance Type:}  &    nonrobust     & \\textbf{                     } &             \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "\\begin{tabular}{lcccccc}\n",
       "                   & \\textbf{coef} & \\textbf{std err} & \\textbf{t} & \\textbf{P$> |$t$|$} & \\textbf{[0.025} & \\textbf{0.975]}  \\\\\n",
       "\\midrule\n",
       "\\textbf{Intercept} &       0.0005  &        0.001     &     0.925  &         0.356        &       -0.001    &        0.002     \\\\\n",
       "\\textbf{SMB}       &       0.9393  &        0.011     &    86.299  &         0.000        &        0.918    &        0.961     \\\\\n",
       "\\textbf{VMG}       &      -0.2150  &        0.014     &   -15.140  &         0.000        &       -0.243    &       -0.187     \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "\\begin{tabular}{lclc}\n",
       "\\textbf{Omnibus:}       & 96.463 & \\textbf{  Durbin-Watson:     } &     1.712  \\\\\n",
       "\\textbf{Prob(Omnibus):} &  0.000 & \\textbf{  Jarque-Bera (JB):  } &  1244.349  \\\\\n",
       "\\textbf{Skew:}          &  1.117 & \\textbf{  Prob(JB):          } & 6.21e-271  \\\\\n",
       "\\textbf{Kurtosis:}      & 13.654 & \\textbf{  Cond. No.          } &      37.0  \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "%\\caption{OLS Regression Results}\n",
       "\\end{center}\n",
       "\n",
       "Notes: \\newline\n",
       " [1] Standard Errors assume that the covariance matrix of the errors is correctly specified."
      ],
      "text/plain": [
       "<class 'statsmodels.iolib.summary.Summary'>\n",
       "\"\"\"\n",
       "                            OLS Regression Results                            \n",
       "==============================================================================\n",
       "Dep. Variable:                  FFSMB   R-squared:                       0.981\n",
       "Model:                            OLS   Adj. R-squared:                  0.981\n",
       "Method:                 Least Squares   F-statistic:                     6568.\n",
       "Date:                Wed, 06 Aug 2025   Prob (F-statistic):          3.63e-216\n",
       "Time:                        17:21:16   Log-Likelihood:                 899.60\n",
       "No. Observations:                 252   AIC:                            -1793.\n",
       "Df Residuals:                     249   BIC:                            -1783.\n",
       "Df Model:                           2                                         \n",
       "Covariance Type:            nonrobust                                         \n",
       "==============================================================================\n",
       "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
       "------------------------------------------------------------------------------\n",
       "Intercept      0.0005      0.001      0.925      0.356      -0.001       0.002\n",
       "SMB            0.9393      0.011     86.299      0.000       0.918       0.961\n",
       "VMG           -0.2150      0.014    -15.140      0.000      -0.243      -0.187\n",
       "==============================================================================\n",
       "Omnibus:                       96.463   Durbin-Watson:                   1.712\n",
       "Prob(Omnibus):                  0.000   Jarque-Bera (JB):             1244.349\n",
       "Skew:                           1.117   Prob(JB):                    6.21e-271\n",
       "Kurtosis:                      13.654   Cond. No.                         37.0\n",
       "==============================================================================\n",
       "\n",
       "Notes:\n",
       "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
       "\"\"\""
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fit1 = ols('FFSMB~SMB+VMG', Ch_ff).fit()\n",
    "fit1.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "f5d727fb-2a5b-4cd3-8c14-144f302f8b29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>OLS Regression Results</caption>\n",
       "<tr>\n",
       "  <th>Dep. Variable:</th>          <td>FFHML</td>      <th>  R-squared:         </th> <td>   0.319</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model:</th>                   <td>OLS</td>       <th>  Adj. R-squared:    </th> <td>   0.313</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Method:</th>             <td>Least Squares</td>  <th>  F-statistic:       </th> <td>   58.30</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Date:</th>             <td>Wed, 06 Aug 2025</td> <th>  Prob (F-statistic):</th> <td>1.71e-21</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Time:</th>                 <td>17:22:20</td>     <th>  Log-Likelihood:    </th> <td>  459.42</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>No. Observations:</th>      <td>   252</td>      <th>  AIC:               </th> <td>  -912.8</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Residuals:</th>          <td>   249</td>      <th>  BIC:               </th> <td>  -902.3</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Model:</th>              <td>     2</td>      <th>                     </th>     <td> </td>   \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Covariance Type:</th>      <td>nonrobust</td>    <th>                     </th>     <td> </td>   \n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "      <td></td>         <th>coef</th>     <th>std err</th>      <th>t</th>      <th>P>|t|</th>  <th>[0.025</th>    <th>0.975]</th>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Intercept</th> <td>   -0.0094</td> <td>    0.003</td> <td>   -2.955</td> <td> 0.003</td> <td>   -0.016</td> <td>   -0.003</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>SMB</th>       <td>    0.0817</td> <td>    0.062</td> <td>    1.309</td> <td> 0.192</td> <td>   -0.041</td> <td>    0.205</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>VMG</th>       <td>    0.7863</td> <td>    0.081</td> <td>    9.655</td> <td> 0.000</td> <td>    0.626</td> <td>    0.947</td>\n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "  <th>Omnibus:</th>       <td>23.095</td> <th>  Durbin-Watson:     </th> <td>   1.998</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Prob(Omnibus):</th> <td> 0.000</td> <th>  Jarque-Bera (JB):  </th> <td>  91.992</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Skew:</th>          <td>-0.113</td> <th>  Prob(JB):          </th> <td>1.06e-20</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Kurtosis:</th>      <td> 5.951</td> <th>  Cond. No.          </th> <td>    37.0</td>\n",
       "</tr>\n",
       "</table><br/><br/>Notes:<br/>[1] Standard Errors assume that the covariance matrix of the errors is correctly specified."
      ],
      "text/latex": [
       "\\begin{center}\n",
       "\\begin{tabular}{lclc}\n",
       "\\toprule\n",
       "\\textbf{Dep. Variable:}    &      FFHML       & \\textbf{  R-squared:         } &     0.319   \\\\\n",
       "\\textbf{Model:}            &       OLS        & \\textbf{  Adj. R-squared:    } &     0.313   \\\\\n",
       "\\textbf{Method:}           &  Least Squares   & \\textbf{  F-statistic:       } &     58.30   \\\\\n",
       "\\textbf{Date:}             & Wed, 06 Aug 2025 & \\textbf{  Prob (F-statistic):} &  1.71e-21   \\\\\n",
       "\\textbf{Time:}             &     17:22:20     & \\textbf{  Log-Likelihood:    } &    459.42   \\\\\n",
       "\\textbf{No. Observations:} &         252      & \\textbf{  AIC:               } &    -912.8   \\\\\n",
       "\\textbf{Df Residuals:}     &         249      & \\textbf{  BIC:               } &    -902.3   \\\\\n",
       "\\textbf{Df Model:}         &           2      & \\textbf{                     } &             \\\\\n",
       "\\textbf{Covariance Type:}  &    nonrobust     & \\textbf{                     } &             \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "\\begin{tabular}{lcccccc}\n",
       "                   & \\textbf{coef} & \\textbf{std err} & \\textbf{t} & \\textbf{P$> |$t$|$} & \\textbf{[0.025} & \\textbf{0.975]}  \\\\\n",
       "\\midrule\n",
       "\\textbf{Intercept} &      -0.0094  &        0.003     &    -2.955  &         0.003        &       -0.016    &       -0.003     \\\\\n",
       "\\textbf{SMB}       &       0.0817  &        0.062     &     1.309  &         0.192        &       -0.041    &        0.205     \\\\\n",
       "\\textbf{VMG}       &       0.7863  &        0.081     &     9.655  &         0.000        &        0.626    &        0.947     \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "\\begin{tabular}{lclc}\n",
       "\\textbf{Omnibus:}       & 23.095 & \\textbf{  Durbin-Watson:     } &    1.998  \\\\\n",
       "\\textbf{Prob(Omnibus):} &  0.000 & \\textbf{  Jarque-Bera (JB):  } &   91.992  \\\\\n",
       "\\textbf{Skew:}          & -0.113 & \\textbf{  Prob(JB):          } & 1.06e-20  \\\\\n",
       "\\textbf{Kurtosis:}      &  5.951 & \\textbf{  Cond. No.          } &     37.0  \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "%\\caption{OLS Regression Results}\n",
       "\\end{center}\n",
       "\n",
       "Notes: \\newline\n",
       " [1] Standard Errors assume that the covariance matrix of the errors is correctly specified."
      ],
      "text/plain": [
       "<class 'statsmodels.iolib.summary.Summary'>\n",
       "\"\"\"\n",
       "                            OLS Regression Results                            \n",
       "==============================================================================\n",
       "Dep. Variable:                  FFHML   R-squared:                       0.319\n",
       "Model:                            OLS   Adj. R-squared:                  0.313\n",
       "Method:                 Least Squares   F-statistic:                     58.30\n",
       "Date:                Wed, 06 Aug 2025   Prob (F-statistic):           1.71e-21\n",
       "Time:                        17:22:20   Log-Likelihood:                 459.42\n",
       "No. Observations:                 252   AIC:                            -912.8\n",
       "Df Residuals:                     249   BIC:                            -902.3\n",
       "Df Model:                           2                                         \n",
       "Covariance Type:            nonrobust                                         \n",
       "==============================================================================\n",
       "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
       "------------------------------------------------------------------------------\n",
       "Intercept     -0.0094      0.003     -2.955      0.003      -0.016      -0.003\n",
       "SMB            0.0817      0.062      1.309      0.192      -0.041       0.205\n",
       "VMG            0.7863      0.081      9.655      0.000       0.626       0.947\n",
       "==============================================================================\n",
       "Omnibus:                       23.095   Durbin-Watson:                   1.998\n",
       "Prob(Omnibus):                  0.000   Jarque-Bera (JB):               91.992\n",
       "Skew:                          -0.113   Prob(JB):                     1.06e-20\n",
       "Kurtosis:                       5.951   Cond. No.                         37.0\n",
       "==============================================================================\n",
       "\n",
       "Notes:\n",
       "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
       "\"\"\""
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fit2 = ols('FFHML~SMB+VMG', Ch_ff).fit()\n",
    "fit2.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "942b2d6e-4add-4763-bcc0-bf5db917f27b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>OLS Regression Results</caption>\n",
       "<tr>\n",
       "  <th>Dep. Variable:</th>           <td>SMB</td>       <th>  R-squared:         </th> <td>   0.965</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model:</th>                   <td>OLS</td>       <th>  Adj. R-squared:    </th> <td>   0.964</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Method:</th>             <td>Least Squares</td>  <th>  F-statistic:       </th> <td>   3391.</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Date:</th>             <td>Wed, 06 Aug 2025</td> <th>  Prob (F-statistic):</th> <td>2.34e-181</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Time:</th>                 <td>17:24:12</td>     <th>  Log-Likelihood:    </th> <td>  830.61</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>No. Observations:</th>      <td>   252</td>      <th>  AIC:               </th> <td>  -1655.</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Residuals:</th>          <td>   249</td>      <th>  BIC:               </th> <td>  -1645.</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Model:</th>              <td>     2</td>      <th>                     </th>     <td> </td>    \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Covariance Type:</th>      <td>nonrobust</td>    <th>                     </th>     <td> </td>    \n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "      <td></td>         <th>coef</th>     <th>std err</th>      <th>t</th>      <th>P>|t|</th>  <th>[0.025</th>    <th>0.975]</th>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Intercept</th> <td>    0.0045</td> <td>    0.001</td> <td>    7.625</td> <td> 0.000</td> <td>    0.003</td> <td>    0.006</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>FFSMB</th>     <td>    0.9407</td> <td>    0.012</td> <td>   79.579</td> <td> 0.000</td> <td>    0.917</td> <td>    0.964</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>FFHML</th>     <td>    0.0185</td> <td>    0.012</td> <td>    1.484</td> <td> 0.139</td> <td>   -0.006</td> <td>    0.043</td>\n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "  <th>Omnibus:</th>       <td>58.760</td> <th>  Durbin-Watson:     </th> <td>   1.729</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Prob(Omnibus):</th> <td> 0.000</td> <th>  Jarque-Bera (JB):  </th> <td> 395.592</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Skew:</th>          <td>-0.702</td> <th>  Prob(JB):          </th> <td>1.25e-86</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Kurtosis:</th>      <td> 8.975</td> <th>  Cond. No.          </th> <td>    24.2</td>\n",
       "</tr>\n",
       "</table><br/><br/>Notes:<br/>[1] Standard Errors assume that the covariance matrix of the errors is correctly specified."
      ],
      "text/latex": [
       "\\begin{center}\n",
       "\\begin{tabular}{lclc}\n",
       "\\toprule\n",
       "\\textbf{Dep. Variable:}    &       SMB        & \\textbf{  R-squared:         } &     0.965   \\\\\n",
       "\\textbf{Model:}            &       OLS        & \\textbf{  Adj. R-squared:    } &     0.964   \\\\\n",
       "\\textbf{Method:}           &  Least Squares   & \\textbf{  F-statistic:       } &     3391.   \\\\\n",
       "\\textbf{Date:}             & Wed, 06 Aug 2025 & \\textbf{  Prob (F-statistic):} & 2.34e-181   \\\\\n",
       "\\textbf{Time:}             &     17:24:12     & \\textbf{  Log-Likelihood:    } &    830.61   \\\\\n",
       "\\textbf{No. Observations:} &         252      & \\textbf{  AIC:               } &    -1655.   \\\\\n",
       "\\textbf{Df Residuals:}     &         249      & \\textbf{  BIC:               } &    -1645.   \\\\\n",
       "\\textbf{Df Model:}         &           2      & \\textbf{                     } &             \\\\\n",
       "\\textbf{Covariance Type:}  &    nonrobust     & \\textbf{                     } &             \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "\\begin{tabular}{lcccccc}\n",
       "                   & \\textbf{coef} & \\textbf{std err} & \\textbf{t} & \\textbf{P$> |$t$|$} & \\textbf{[0.025} & \\textbf{0.975]}  \\\\\n",
       "\\midrule\n",
       "\\textbf{Intercept} &       0.0045  &        0.001     &     7.625  &         0.000        &        0.003    &        0.006     \\\\\n",
       "\\textbf{FFSMB}     &       0.9407  &        0.012     &    79.579  &         0.000        &        0.917    &        0.964     \\\\\n",
       "\\textbf{FFHML}     &       0.0185  &        0.012     &     1.484  &         0.139        &       -0.006    &        0.043     \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "\\begin{tabular}{lclc}\n",
       "\\textbf{Omnibus:}       & 58.760 & \\textbf{  Durbin-Watson:     } &    1.729  \\\\\n",
       "\\textbf{Prob(Omnibus):} &  0.000 & \\textbf{  Jarque-Bera (JB):  } &  395.592  \\\\\n",
       "\\textbf{Skew:}          & -0.702 & \\textbf{  Prob(JB):          } & 1.25e-86  \\\\\n",
       "\\textbf{Kurtosis:}      &  8.975 & \\textbf{  Cond. No.          } &     24.2  \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "%\\caption{OLS Regression Results}\n",
       "\\end{center}\n",
       "\n",
       "Notes: \\newline\n",
       " [1] Standard Errors assume that the covariance matrix of the errors is correctly specified."
      ],
      "text/plain": [
       "<class 'statsmodels.iolib.summary.Summary'>\n",
       "\"\"\"\n",
       "                            OLS Regression Results                            \n",
       "==============================================================================\n",
       "Dep. Variable:                    SMB   R-squared:                       0.965\n",
       "Model:                            OLS   Adj. R-squared:                  0.964\n",
       "Method:                 Least Squares   F-statistic:                     3391.\n",
       "Date:                Wed, 06 Aug 2025   Prob (F-statistic):          2.34e-181\n",
       "Time:                        17:24:12   Log-Likelihood:                 830.61\n",
       "No. Observations:                 252   AIC:                            -1655.\n",
       "Df Residuals:                     249   BIC:                            -1645.\n",
       "Df Model:                           2                                         \n",
       "Covariance Type:            nonrobust                                         \n",
       "==============================================================================\n",
       "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
       "------------------------------------------------------------------------------\n",
       "Intercept      0.0045      0.001      7.625      0.000       0.003       0.006\n",
       "FFSMB          0.9407      0.012     79.579      0.000       0.917       0.964\n",
       "FFHML          0.0185      0.012      1.484      0.139      -0.006       0.043\n",
       "==============================================================================\n",
       "Omnibus:                       58.760   Durbin-Watson:                   1.729\n",
       "Prob(Omnibus):                  0.000   Jarque-Bera (JB):              395.592\n",
       "Skew:                          -0.702   Prob(JB):                     1.25e-86\n",
       "Kurtosis:                       8.975   Cond. No.                         24.2\n",
       "==============================================================================\n",
       "\n",
       "Notes:\n",
       "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
       "\"\"\""
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fit3=ols('SMB~FFSMB+FFHML',Ch_ff).fit()\n",
    "fit3.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5adfc08-9dd0-4c53-aba8-e4b76b591548",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c27d3a0-b33e-41d1-b507-1f3d86bb9ab6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
